package com.heitian.ssm.controller;

import com.google.gson.JsonObject;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.model.User;
import com.heitian.ssm.service.UserService;
import com.heitian.ssm.utils.CommonUtils;
import com.heitian.ssm.utils.GsonUtil;
import com.heitian.ssm.utils.RegexUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者： 陈建峰
 * 创建时间： 17/10/16 下午4:13
 * 版本： [1.0, 17/10/16]
 * 描述： 用户处理
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private Logger log = Logger.getLogger(UserController.class);

    @Resource
    private UserService userService;

    /**
     * 用户登录
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/login",method=RequestMethod.POST)
    public void login(HttpServletRequest request,HttpServletResponse response) throws IOException{
        log.info("用户登录");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String LoginID = paras.get("LoginID").getAsString();
        String PassWord = paras.get("PassWord").getAsString();
        //将用户名和密码传递给service层
        User user = userService.login(LoginID, PassWord);
        String UserArea = null;
        String code = "0";
        if (user != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(user);
        }
        response.getWriter().write(CommonUtils.createJson(code, "用户名密码错误！", UserArea).toString());
    }

    /**
     * 修改用户密码
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/setNewPwd",method=RequestMethod.POST)
    public void setNewPwd(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String UserGuid = paras.get("UserGuid").getAsString();
        String OldPassword = paras.get("OldPassword").getAsString();
        String NewPassword = paras.get("NewPassword").getAsString();
        //将用户名和密码传递给service层
        User user = userService.getUserByUserGuid(UserGuid);
        if (!CommonUtils.md5(OldPassword).equals(user.getPassWord())) {
            response.getWriter().write(CommonUtils.createJson("0", "老密码不正确！", null).toString());
            return;
        }
        userService.setNewPassWord(user, CommonUtils.md5(NewPassword));
        response.getWriter().write(CommonUtils.createJson("1", "", null).toString());
    }

    /**
     * 通过userguid获取用户信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/getUserInfoByUserGuid",method=RequestMethod.POST)
    public void getUserInfoByUserGuid(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String UserGuid = paras.get("UserGuid").getAsString();
        //将用户名和密码传递给service层
        User user = userService.getUserByUserGuid(UserGuid);
        String UserArea = null;
        String code = "0";
        if (user != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(user);
        }
        response.getWriter().write(CommonUtils.createJson(code, "", UserArea).toString());
    }

    /**
     * 分页获取人员信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/getAllUser",method=RequestMethod.POST)
    public void getAllUser(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String userName = paras.get("userName").getAsString();
        String currentPage = paras.get("currentPage").getAsString();
        String pageCount = paras.get("pageCount").getAsString();
        if(userName == null) userName = "";
        if (currentPage == null) currentPage = "1";
        if (pageCount == null) pageCount = "10";
        int currentPageIndex = Integer.parseInt(currentPage);
        int currentPageCount = Integer.parseInt(pageCount);
        PageBean pageBean = userService.getAllUser(userName,currentPageIndex, currentPageCount);
        String UserArea = null;
        String code = "0";
        String message = "";
        if (pageBean != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(pageBean);
        } else {
            message = "用户数据获取失败！";
        }
        response.getWriter().write(CommonUtils.createJson(code, message, UserArea).toString());
    }

    /**
     * 删除用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/DeletelUser",method=RequestMethod.POST)
    public void DeletelUser(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String UserGuids = paras.get("UserGuids").getAsString();
        String[] userguids = UserGuids.split(";");
        for (int i = 0; i < userguids.length; i++) {
            userService.deleteUser(userguids[i]);
        }
        response.getWriter().write(CommonUtils.createJson("1", "删除用户成功！", null).toString());
    }

    /**
     * 注册用户
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/register",method=RequestMethod.POST)
    public void register(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String DisplayName = paras.get("DisplayName").getAsString();
        String PassWord = paras.get("PassWord").getAsString();
        String LoginID = paras.get("LoginID").getAsString();
        String email = paras.get("email").getAsString();
        String telephone = paras.get("telephone").getAsString();
        String birthday = paras.get("birthday").getAsString();
        String sex = paras.get("sex").getAsString();
        String PhotoUrl = paras.get("PhotoUrl").getAsString();
        if(RegexUtil.isNull(DisplayName)){
            response.getWriter().write(CommonUtils.createJson("0", "请正确填写用户名！", null).toString());
            return;
        }
        if(RegexUtil.isNull(PassWord)){
            response.getWriter().write(CommonUtils.createJson("0", "请正确填写密码！", null).toString());
            return;
        }
        if(RegexUtil.isNull(LoginID)){
            response.getWriter().write(CommonUtils.createJson("0", "请正确填写登录名！", null).toString());
            return;
        }
        User user = userService.getUserByLoginID(LoginID);
        if(user!=null){
            response.getWriter().write(CommonUtils.createJson("0", "登录名已被注册！", null).toString());
            return;
        }
        User newUser = new User();
        newUser.setUserGuid(CommonUtils.GetUUID());
        newUser.setDisplayName(DisplayName);
        newUser.setBirthday(birthday);
        newUser.setCode("0");
        newUser.setEmail(email);
        newUser.setLoginID(LoginID);
        newUser.setPassWord(PassWord);
        newUser.setPhotoUrl(PhotoUrl);
        newUser.setSex(sex);
        newUser.setTelephone(telephone);
        newUser.setIsadmin("0");
        newUser.setIsSuperAdmin("0");
        newUser.setState("0");
        userService.register(newUser);
        response.getWriter().write(CommonUtils.createJson("1", "新增用户成功！", GsonUtil.GsonString(newUser)).toString());
    }
}
